<template>
	<view class="page">
		<view class="form">
			<view class="form-item">
				<view class="title">
					请上传图片<text style="font-weight: normal;">（选填）</text>：
				</view>
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
					:maxCount="10"></u-upload>
			</view>
			<view class="form-item">
				<view class="title">
					请输入内容：
				</view>
				<u-textarea v-model="text" placeholder="输入内容..." count maxlength="200"></u-textarea>
			</view>
		</view>
		<view class="submit-tc">
		</view>
		<view class="submit flexAJC" @click="submit">
			提交
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileList1: [],
				text: ''
			};
		},
		onLoad() {

		},
		methods: {
			submit() {
				if (uni.$u.test.isEmpty(this.text)) {
					uni.showToast({
						title: "反馈内容不能为空",
						icon: "none"
					})
					return false
				}
				this.$api.user.yijian({
					content: this.text,
					images: this.fileList1.map(i => {
						return i.url
					}).join(',')
				}).then(res => {
					uni.showToast({
						title: "提交成功",
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)


				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$serverUrl + '/api/upload/uploadImg', //仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data).data.image)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.form {
		width: 100%;
		padding: 0 32rpx 0;

		.form-item {
			padding: 50rpx 0 0;

			.title {
				font-weight: bold;
				font-size: 28rpx;
				margin-bottom: 36rpx;
			}
		}
	}

	.submit-tc {
		height: 200rpx;
	}

	.submit {
		width: 500rpx;
		height: 110rpx;
		background: #F4BD38;
		border-radius: 55rpx;
		color: #FFFFFF;
		font-weight: bold;
		font-size: 26rpx;
		position: fixed;
		bottom: 70rpx;
		left: 50%;
		transform: translateX(-50%);
	}
</style>